package com.atxm.mimall.product.service.impl;

import com.atxm.mimall.product.entity.*;
import com.atxm.mimall.product.service.*;
import com.atxm.mimall.product.vo.Attr;
import com.atxm.mimall.product.vo.AttrGroupWithAttrsVo;
import io.prometheus.client.Collector;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atxm.common.utils.PageUtils;
import com.atxm.common.utils.Query;

import com.atxm.mimall.product.dao.AttrGroupDao;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryByPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if( catelogId != 0){
            wrapper.eq("catelog_id",catelogId);
        }
        wrapper.orderByAsc("sort");
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        List<Long> collect = page.getRecords().stream().map(v -> v.getCatelogId()).collect(Collectors.toList());
        if (collect != null && collect.size() >0) {
            List<CategoryEntity> categoryEntity = categoryService.list(new QueryWrapper<CategoryEntity>().in("cat_id", collect));
            Map<Long, String> collect1 = categoryEntity.stream().collect(Collectors.toMap(v -> v.getCatId(), v -> v.getName()));
            page.getRecords().stream().forEach(v -> {
                if (collect1.containsKey(v.getCatelogId()))
                    v.setCatelogName(collect1.get(v.getCatelogId()));
            });
        }
//        page.getRecords().stream().sorted(Comparator.comparing(v->v.getSort())).collect(Collectors.toList());
        return new PageUtils(page);
    }

    @Override
    public void updateByCatelogId(AttrGroupEntity attrGroup) {
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrGroup.getAttrGroupId());
        if (attrGroupEntity.getCatelogId().longValue() != attrGroup.getCatelogId().longValue()){
            attrAttrgroupRelationService.remove(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id",attrGroup.getAttrGroupId()));
        }
        baseMapper.updateById(attrGroup);
    }

    /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1、查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;


    }

}